<?php

/*
 * Fresns Control Panel (Standalone)
 * Copyright (C) 2021-Present 刘鑫
 * Released under the MIT License.
 */

namespace App\Plugins\FcpApi\TweetEmojis;

use App\Base\Controllers\BaseAdminController;
use App\Helpers\CommonHelper;
use App\Http\Center\Common\ErrorCodeService;
use App\Plugins\FcpApi\Base\TweetBaseController;
use Illuminate\Http\Request;

class FsControllerAdmin extends TweetBaseController
{
    public function __construct()
    {
        parent::__construct();
        $this->service = new FsService();
    }

    public function index(Request $request)
    {
        $parentId = $request->input('parent_id');
        if (empty($parentId)) {
            $request->offsetSet('type', FsConfig::TYPE_GROUP);
        }

        parent::index($request);
    }

    public function store(Request $request)
    {
        $request->offsetSet('name', '#');
        // $request->offsetSet('code', '#');
        parent::store($request);
    }

    // 删除
    public function destroy(Request $request)
    {
        //验证
        $ids = $request->input('ids');
        $idArr = explode(',', $ids);

        //判断是否有子类
        $count = FsModel::whereIn('parent_id', $idArr)->count();
        if ($count > 0) {
            $this->errorInfo(1, '存在子类，不允许删除');
        }
        // 执行
        $this->service->destroy($idArr);

        // 清空request数据
        CommonHelper::removeRequestFields($this->service->getSearchableFields());

        $this->index($request);
    }

    // 验证规则
    public function rules($ruleType)
    {
        $rule = [];

        $config = new FsConfig($this->service->getTable());

        switch ($ruleType) {
            case FsConfig::RULE_STORE:
                $rule = $config->storeRule();
                break;

            case FsConfig::RULE_UPDATE:
                $rule = $config->updateRule();
                break;

            case FsConfig::RULE_DESTROY:
                $rule = $config->destroyRule();
                break;

            case FsConfig::RULE_DETAIL:
                $rule = $config->detailRule();
                break;
        }

        return $rule;
    }
}
